package com.zy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.bean.po.Edge;
import com.zy.mapper.EdgeMapper;
import com.zy.parser.logicflow.LfEdge;
import com.zy.service.EdgeService;
import com.zy.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hanhuafeng
 * @createDate 2023-05-31 15:37:36
 * @description 流程连接线表
 */
@Service
@RequiredArgsConstructor
public class EdgeServiceImpl extends ServiceImpl<EdgeMapper, Edge>
        implements EdgeService {
    private final UserUtil userUtil;

    /**
     * 更新流程连接线
     *
     * @param updateEdges 更新的连接线
     * @param chartCode   流程图编码
     * @param versionCode 版本编码
     */
    @Override
    public void updateEdgeByLfEdge(List<LfEdge> updateEdges, String chartCode, String versionCode) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        for (LfEdge edge : updateEdges) {
            Edge e = new Edge();
            e.setEdgeCode(edge.getId());
            e.setType(edge.getType());
            e.setSourceNodeCode(edge.getSourceNodeId());
            e.setTargetNodeCode(edge.getTargetNodeId());
            e.setStartPoint(om.writeValueAsString(edge.getStartPoint()));
            e.setEndPoint(om.writeValueAsString(edge.getEndPoint()));
            e.setProperties(om.writeValueAsString(edge.getProperties()));
            e.setPointsList(om.writeValueAsString(edge.getPointsList()));
            e.setChartCode(chartCode);
            e.setChartVersionCode(versionCode);
            e.setCreateUserId(userUtil.getUserCode());
            LambdaQueryWrapper<Edge> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Edge::getEdgeCode, edge.getId());
            lqw.eq(Edge::getChartCode, chartCode);
            lqw.eq(Edge::getChartVersionCode, versionCode);
            this.update(e, lqw);
        }
    }
}




